<template>
	<view>
		<u-navbar back-text="返回" title="书架"></u-navbar>
		<view class="cu-bar">
			<view class="action">
				<text>共有{{DataList.length}}本</text>
			</view>
			<view class="action">
				<text @click="editClick">编辑</text>
			</view>
		</view>
		<view class="grid col-3 canui-list-box">
			<view class="padding-sm" v-for="(item,index) in DataList" :key="index" @click="readClick(item)">
				<image :src="item.img" mode="scaleToFill" class="radius"></image>
				<view>{{item.title}}</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				DataList: [],
			}
		},
		onLoad() {
			//this.GetBookList();
		},
		onShow() {
			this.GetBookList();
			console.info(this.test(1595817049000))
		},
		methods: {
			format(date, format) {
				var o = {
					"M+": date.getMonth() + 1,
					"d+": date.getDate(),
					"h+": date.getHours(),
					"m+": date.getMinutes(),
					"s+": date.getSeconds(),
					"q+": Math.floor((date.getMonth() + 3) / 3),
					"S": date.getMilliseconds()
				}
				if (/(y+)/.test(format)) {
					format = format.replace(RegExp.$1, (date.getFullYear() + "")
						.substr(4 - RegExp.$1.length));
				}
				for (var k in o) {
					if (new RegExp("(" + k + ")").test(format)) {
						format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] :
							("00" + o[k]).substr(("" + o[k]).length));
					}
				}
				return format;
			},
			test(time) {
				var now = new Date()
				now.setHours(0, 0, 0);
				var today_start = now.getTime();
				if (time >= today_start) {
					return this.format(new Date(time), "hh:mm");
				}
				var dateForYestd = new Date()
				var yestdTime = Math.floor(dateForYestd.getTime() / 1000) - 86400
				var yestd_time = new Date(yestdTime * 1000)
				yestd_time.setHours(0, 0, 0)
				var yestd_start = yestd_time.getTime()
				if (time >= yestd_start) {
					return "昨天";
				}
				now = new Date()
				var nowDay = now.getDate();
				var nowMonth = now.getMonth();
				var nowYear = now.getFullYear();
				var nowDayOfWeek = now.getDay();
				var weekStart = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek + 1, 0, 0, 0).getTime()
				if (time >= weekStart) {
					return "周" + nowDayOfWeek;
				}
				var lastweekStart = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 6, 0, 0, 0).getTime()
				if (time >= lastweekStart) {
					return this.format(new Date(time), "yy/MM/dd");
				}
				return "null";
			},
			GetBookList() {
				this._tool.GetBookListData().then((res) => {
					console.info(res)
					if (res == "" || res == null || res == undefined) {
						this.DataList = [];
					} else {
						this.DataList = res;
					}
				});
			},
			editClick() {
				uni.navigateTo({
					url: '/pages/bookshelf/edit'
				})
			},
			readClick(bookData) {
				try {
					uni.setStorageSync('BookReadData', bookData);
					uni.navigateTo({
						url: '/pages/info/info?id=' + bookData.id
					})
				} catch (e) {
					uni.showToast({
						title: '数据异常',
						icon: 'none'
					});
				}
			}
		}
	}
</script>

<style lang="less">
	.canui-list-box {
		image {
			height: 300rpx;
			background-image: url(/static/img/book_no_pic.jpg);
			background-repeat: no-repeat;
			background-size: 100% 100%;
			-moz-background-size: 100% 100%;
		}
	}
</style>
